* Set current directory and open log
cd "C:\Users\frank.haege\Documents\Research\EUPOL\Version 5\Data analysis"
capture log close
log using "prlx-v05-analysis01", replace text


**************************************************************************************
* Figures and tables for descriptive graphs and tables in Excel file (updated to 2014)
**************************************************************************************


* Project and author information
********************************
* Project:		prlx
* Programme:	prlx-v05-analysis01.do
* Date:			13 October 2014
* Author:		Frank Haege, University of Limerick
* Contact:		frank.haege@ul.ie 

* Do-file description
*********************
* This do-file generates descriptive figures and tables presented in prlx-v05-tables-141013.xls.
* It relies on the variables created by the do-file "prlx-v05-management02.do".


* Clear memory  
**************
version 12.0
clear all
macro drop _all
set more off

* Load dataset with type of instrument and type of legislative procedure variables
use "prlx-v05-management02.dta", clear
des, short


* Figure 1: Number of documents by type of file
***********************************************

* Calculate proportions of different types of files
generate ones = 1
egen instones = sum(ones), by(instrument)
generate instprop = instones/_N

* Generate new instrument variable with small categories merged into 'Other' category
generate instrumentp = instrument
replace instrumentp = 34 if instprop < 0.01
label val instrumentp instrumentl
tab instrument, sort m
tab instrumentp, sort m

* Plot number of documents by type of file
graph dot (sum) ones, over(instrumentp, sort(1) descending) /*
	*/ ytitle("Number of Documents", margin(medsmall)) /*
	*/ marker(1, mcolor(black)) scale(1.1)
	
	
* Table SI-1: Number of documents by type of file
*************************************************

* Tabulate instrument variable
tab instrument, sort m
	
	
* Figure 2: Number of proposals by type of legislative procedure
****************************************************************	

* Calculate proportions of types of legislative procedures
egen procones = sum(ones), by(procedurec)
egen nonmiss = sum(ones) if procedurec != 0
generate procprop = procones/nonmiss if procedurec != 0 

* Generate new procedure variable with small categories merged into "Other"
generate procedurecp = procedurec if procedurec != 0
replace procedurecp = 10 if procprop < 0.005 & procedurec != 0
label val procedurecp procedurel
tab procedurecp if procedurec != 0, m sort

* Plot number of proposals by type of legislative procedure
graph dot (sum) ones if procedurec != 0 /*
	*/ , over(procedurecp, sort(1) descending) /*
	*/ ytitle("Number of Proposals", margin(medsmall)) /*
	*/ marker(1, mcolor(black)) scale(1.1) xsize(4)
	
	
* Table SI-2.1: Number of proposals by type of legislative procedure
********************************************************************

* Tabulate procedure variable
tab procedurec if procedurec != 0, m sort


* Table SI-2.2: Number of proposals by type of legislative procedure (including "no procedure")
***********************************************************************************************

* Tabulate procedure variable
tab procedurec, m sort


* Table SI-3: Number of non-legislative documents by type of file, 1976-2012
****************************************************************************

* Replace missing year values by year information from proposal code
replace year = year2 if year == . & year2 != .

* Check missing instrument values
tab instrument procedurec, m
list webno title if instrument == .	
* Most observations with missing instrument values were not subject to a legislative procedure
* The observations with a legislative procedure do not specify the title of the proposal 
* (thus no way of knowing what the instrument was)

* Label year variable
label var year "Year"

* Tabulate the number of non-legislative documents by type of file
tab year instrument if instrument == 4 | instrument == 22 | instrument == 23


* Table SI-4: Number of legislative proposals by type of file, 1976-2012
************************************************************************

* Tabulate the number of legislative proposals by type of file
tab year instrument if procedurec == 1 | procedurec == 2 | procedurec == 3


* Generate Table SI-5 Number of legislative proposals by type of procedure, 1976-2012
*************************************************************************************

* Tabulate the number of legislative proposals by type of procedure
tab year procedurec if procedurec < 4 & procedurec != 0


* Collapse dataset to generate the remaining graphs
***************************************************

* Generate instrument dummy variables
tab instrument, m gen(inst_)

* Generate instrument dummy variables for legislative acts decided through main three procedures
* Decisions
generate inst_1c = 0
replace inst_1c = 1 if inst_1 == 1 & (procedurec == 1 | procedurec == 2 | procedurec == 3)
* Regulations
generate inst_2c = 0
replace inst_2c = 1 if inst_2 == 1 & (procedurec == 1 | procedurec == 2 | procedurec == 3)
* Directives
generate inst_3c = 0
replace inst_3c = 1 if inst_3 == 1 & (procedurec == 1 | procedurec == 2 | procedurec == 3)

* Generate procedure dummy variable
tab procedurec, m gen(proc_)

* Collapse dataset by year
collapse (sum) inst_* proc_*, by(year)

* Label new variables
label var inst_23 "Communications"
label var inst_4 "Reports"
label var inst_22 "Working papers"
label var inst_1c "Decisions"
label var inst_2c "Regulations"
label var inst_3c "Directives"
label var proc_2 "Consultation"
label var proc_3 "Cooperation"
label var proc_4 "Codecision"


* Figure 3: Number of non-legislative documents by type of file, 1976-2012
**************************************************************************

* Replace zeros for number of working papers before 1996 by missing values
list year inst_23 inst_4 inst_22
replace inst_22 = . if year < 1996	

* Check that Figure 3 is based on numbers given in Table SI-3
list year inst_23 inst_4 inst_22

* Plot the number of non-legislative documents by type of file
twoway line inst_23 inst_4 inst_22 year, /*
	*/ xlabel(1975 1980:2015) ylabel(0 50:250) xscale(range(1975 2010)) /*
	*/ ytitle("Number of documents") /*
	*/ lpattern("-..."l  _) lcolor(red green blue) /*
	*/ legend(cols(3) region(lcolor(none))) 
graph export prlx-v04-analysis01-fig3.png, replace

	
* Figure 4: Number of legislative proposals by type of file, 1976-2012
**********************************************************************

* Check that Figures 4, SI-4.1, and SI-4.2 are based on the numbers given in Table SI-4
list year inst_1c inst_2c inst_3c

* Plot the number of legislative proposals by type of file
twoway line inst_1c inst_2c inst_3c year, /*
	*/ xlabel(1975 1980:2015) ylabel(0 50:250) xscale(range(1975 2010)) /*
	*/ ytitle("Number of proposals") /*
	*/ lpattern("-..." _ l) lcolor(red blue green) /*
	*/ legend(cols(3) region(lcolor(none)))
graph export prlx-v04-analysis01-fig4.png, replace
	
	
* Figure SI-4.1: Number of proposals for decisions, 1976-2012
*************************************************************

* Plot number of proposals for decisions over time
twoway line inst_1c year, ylabel(0 20:120) ytitle("Number of proposals") /*
	*/ xlabel(1975 1980:2015) lcolor(black) title("Decisions")


* Figure SI-4.2: Number of proposals for directives, 1976-2012
**************************************************************

* Summary statistics for yearly number of proposals for directives
sum inst_3c, d

* Plot number of proposals for directives over time
twoway line inst_3c year, ylabel(0 20:100) ytitle("Number of proposals") /*
	*/ xlabel(1975 1980:2015) lcolor(black) title("Directives")
	
	
* Figure SI-4.3: Number of proposals for regulations, 1976-2012
***************************************************************

* Plot number of proposals for regulations over time
twoway line inst_2c year, ylabel(0 50:250) ytitle("Number of proposals") /*
	*/ xlabel(1975 1980:2015) lcolor(black) title("Regulations")
	

* Figure 5: Number of legislative proposals by type of procedure, 1976-2012
***************************************************************************
 
* Replace zeroes of cooperation variable before 1987 by missing values 
list year proc_2 proc_3 proc_4
replace proc_3 = . if year < 1987

* Replace zeroes of codecision variable before 1993 by missing values
replace proc_4 = . if year < 1993
list year proc_2 proc_3 proc_4
	
* Check that Figure 5 is based on numbers given in Table SI-5
list year proc_2 proc_3 proc_4

* Plot number of legislative proposals by type of procedure
twoway line proc_2 proc_3 proc_4 year, /*
	*/ xlabel(1975 1980:2015) ylabel(0 50:300) xscale(range(1975 2010)) /*
	*/ ytitle("Number of proposals") /*
	*/ lpattern(_ "-..." l) lcolor(blue red green) /*
	*/ legend(cols(3) region(lcolor(none)))
graph export prlx-v04-analysis01-fig5.png, replace

	
* Exit do-file
log close
exit
